package com.mind.customer.utils;

import com.mind.common.utils.StringUtils;
import com.mind.customer.config.FileConfig;
import lombok.Data;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@Component
public class OOSUtils {
    @Resource
    private FileConfig fileConfig;

    public boolean upload(byte[] io, String url) throws IOException {
        OOSVo oosVo = getOOS(url);
        Files.write(Paths.get(oosVo.getFilePath()), io);
//        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
//        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
//        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        return true;
    }


    public boolean download(String url){
        return true;
    }


    public OOSVo getOOS(String url){
        if(StringUtils.isEmpty(url)){
            return null;
        }
        String[] urlArr = url.split("/");
        if(urlArr.length <2){
            return null;
        }
        OOSVo oos = new OOSVo();
        oos.setBucketName(urlArr[urlArr.length-2]);
        oos.setObjectName(urlArr[urlArr.length-1]);
        oos.setFilePath(Paths.get(fileConfig.getBasePath(), oos.getObjectName()).toString());
        return oos;
    }

    @Data
    public static class OOSVo{
        private String bucketName;
        private String objectName;
        private String filePath;
    }
}
